Sensor Fusion for Kinetis MCUs (ISSDK/KSDK version)
main_agm01_freertos_two_tasks.c File Reference
+ Include dependency graph for main_agm01_freertos_two_tasks.c:

Go to the source code of this file.

Functions

static void read_task (void *pvParameters)
 
static void fusion_task (void *pvParameters)
 
int main (void)
 

Variables

SensorFusionGlobals sfg
 
ControlSubsystem controlSubsystem
 
StatusSubsystem statusSubsystem
 
PhysicalSensor sensors [2]
 
EventGroupHandle_t event_group = NULL
 

Detailed Description

This file shows one recommended way to incorporate sensor fusion capabilities into a FreeRTOS project.

Definition in file main_agm01_freertos_two_tasks.c.

Function Documentation

static void fusion_task ( void *  pvParameters)
static

Definition at line 117 of file main_agm01_freertos_two_tasks.c.

Referenced by main().

118 {
119  uint16_t i=0; // general counter variable
120  while (1)
121  {
122  xEventGroupWaitBits(event_group, /* The event group handle. */
123  B0, /* The bit pattern the event group is waiting for. */
124  pdTRUE, /* BIT_0 and BIT_4 will be cleared automatically. */
125  pdFALSE, /* Don't wait for both bits, either bit unblock task. */
126  portMAX_DELAY); /* Block indefinitely to wait for the condition to be met. */
127 
128  sfg.conditionSensorReadings(&sfg); // magCal is run as part of this
129  sfg.runFusion(&sfg); // Run the actual fusion algorithms
130  sfg.applyPerturbation(&sfg); // apply debug perturbation (testing only)
131 
132  sfg.loopcounter++; // The loop counter is used to "serialize" mag cal operations
133  i=i+1;
134  if (i>=4) { // Some status codes include a "blink" feature. This loop
135  i=0; // should cycle at least four times for that to operate correctly.
136  sfg.updateStatus(&sfg); // This is where pending status updates are made visible
137  }
138  sfg.queueStatus(&sfg, NORMAL); // assume NORMAL status for next pass through the loop
139  sfg.pControlSubsystem->stream(&sfg, sUARTOutputBuffer); // Send stream data to the Sensor Fusion Toolbox
140  }
141 }
int32_t loopcounter
counter incrementing each iteration of sensor fusion (typically 25Hz)
conditionSensorReadings_t * conditionSensorReadings
preprocessing step for sensor fusion
streamData_t * stream
function to create packets for serial stream
Definition: control.h:73
struct ControlSubsystem * pControlSubsystem
#define B0
Definition: sensor_fusion.h:88
updateStatus_t * updateStatus
status=next status
SensorFusionGlobals sfg
This is the primary sensor fusion data structure.
uint8_t sUARTOutputBuffer[256]
main output buffer defined in control.c
Definition: control.c:59
runFusion_t * runFusion
run the fusion routines
setStatus_t * queueStatus
queue status change for next regular interval
EventGroupHandle_t event_group
applyPerturbation_t * applyPerturbation
apply step function for testing purposes
Operation is Nominal.

+ Here is the caller graph for this function:

int main ( void  )

This is a FreeRTOS (dual task) implementation of the NXP sensor fusion demo build.

Definition at line 72 of file main_agm01_freertos_two_tasks.c.

73 {
74  ARM_DRIVER_I2C* I2Cdrv = &I2C_S_DRIVER_BLOCKING; // defined in the <shield>.h file
75  BOARD_InitPins(); // defined in pin_mux.c, initializes pkg pins
76  BOARD_BootClockRUN(); // defined in clock_config.c, initializes clocks
77  BOARD_InitDebugConsole(); // defined in board.c, initializes the OpenSDA port
78 
79  I2Cdrv->Initialize(NULL); // Initialize the KSDK driver for the I2C port
80 
81  I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST); // Configure the I2C bus speed
82 
83  initializeControlPort(&controlSubsystem); // configure pins and ports for the control sub-system
84  initializeStatusSubsystem(&statusSubsystem); // configure pins and ports for the status sub-system
85  initSensorFusionGlobals(&sfg, &statusSubsystem, &controlSubsystem); // Initialize sensor fusion structures
86  // "install" the sensors we will be using
87  sfg.installSensor(&sfg, &sensors[0], FXOS8700_I2C_ADDR, 1, (void*) I2Cdrv, FXOS8700_Init, FXOS8700_Read);
88  sfg.installSensor(&sfg, &sensors[1], FXAS21002_I2C_ADDR, 1, (void*) I2Cdrv, FXAS21002_Init, FXAS21002_Read);
89  sfg.initializeFusionEngine(&sfg); // This will initialize sensors and magnetic calibration
90 
91  event_group = xEventGroupCreate();
92  xTaskCreate(read_task, "READ", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL);
93  xTaskCreate(fusion_task, "FUSION", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);
94 
95  sfg.setStatus(&sfg, NORMAL); // If we got this far, let's set status state to NORMAL
96  vTaskStartScheduler(); // Start the RTOS scheduler
97  sfg.setStatus(&sfg, HARD_FAULT); // If we got this far, FreeRTOS does not have enough memory allocated
98  for (;;) ;
99 }
void initSensorFusionGlobals(SensorFusionGlobals *sfg, StatusSubsystem *pStatusSubsystem, ControlSubsystem *pControlSubsystem)
utility function to insert default values in the top level structure
Definition: sensor_fusion.c:68
static void read_task(void *pvParameters)
int8_t FXOS8700_Read(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
void initializeStatusSubsystem(StatusSubsystem *pStatus)
initializeStatusSubsystem() should be called once at startup to initialize the data structure and to ...
Definition: status.c:185
installSensor_t * installSensor
function for installing a new sensor into t
static void fusion_task(void *pvParameters)
initializeFusionEngine_t * initializeFusionEngine
set sensor fusion structures to initial values
StatusSubsystem statusSubsystem
provides visual (usually LED) status indicator
int8_t FXOS8700_Init(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
int8_t FXAS21002_Read(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
SensorFusionGlobals sfg
This is the primary sensor fusion data structure.
int8_t initializeControlPort(ControlSubsystem *pComm)
Initialize the control subsystem and all related hardware.
Definition: control.c:182
ARM_DRIVER_I2C * I2Cdrv
KSDK handle for the I2C port defined in Driver_I2C_KSDK2.c.
Non-recoverable FAULT = something went very wrong.
ControlSubsystem controlSubsystem
used for serial communications
PhysicalSensor sensors[2]
This implementation uses two physical sensors.
EventGroupHandle_t event_group
setStatus_t * setStatus
change status indicator immediately
int8_t FXAS21002_Init(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
Operation is Nominal.

+ Here is the call graph for this function:

static void read_task ( void *  pvParameters)
static

Definition at line 101 of file main_agm01_freertos_two_tasks.c.

Referenced by main().

102 {
103  uint16_t i=0; // general counter variable
104  portTickType lastWakeTime;
105  const portTickType frequency = 1; // tick counter runs at the read rate
106  lastWakeTime = xTaskGetTickCount();
107  while (1)
108  {
109  for (i=1; i<=OVERSAMPLE_RATE; i++) {
110  vTaskDelayUntil(&lastWakeTime, frequency);
111  sfg.readSensors(&sfg, i); // Reads sensors, applies HAL and does averaging (if applicable)
112  }
113  xEventGroupSetBits(event_group, B0);
114  }
115 }
#define OVERSAMPLE_RATE
readSensors_t * readSensors
read all physical sensors
#define B0
Definition: sensor_fusion.h:88
SensorFusionGlobals sfg
This is the primary sensor fusion data structure.
EventGroupHandle_t event_group

+ Here is the caller graph for this function:

Variable Documentation

ControlSubsystem controlSubsystem

used for serial communications

Definition at line 63 of file main_agm01_freertos_two_tasks.c.

EventGroupHandle_t event_group = NULL

Definition at line 66 of file main_agm01_freertos_two_tasks.c.

Referenced by fusion_task(), main(), and read_task().

PhysicalSensor sensors[2]

This implementation uses two physical sensors.

Definition at line 65 of file main_agm01_freertos_two_tasks.c.

This is the primary sensor fusion data structure.

Definition at line 62 of file main_agm01_freertos_two_tasks.c.

StatusSubsystem statusSubsystem

provides visual (usually LED) status indicator

Definition at line 64 of file main_agm01_freertos_two_tasks.c.